<template>
  <div>
   <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container" v-if="article!=null">
      <!-- 文章标题 -->
      <h1 class="art-title">{{article.title}}</h1>
      <!-- 用户信息 -->
      <van-cell center :title="article.aut_name" :label="article.pubdate">
        <template #icon>
          <!-- 头像 -->
          <img :src="article.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <!-- 是否关注了作者 -->
            <van-button type="info" size="mini" v-if="article.is_followed" @click="unfollowUser">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="setFollow">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="article.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if='article.attitude===1'  @click="delLike">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-if='article.attitude===-1'   @click="addLike">点赞</van-button>
      </div>
    </div>

    <!-- 文章评论区域 -->
    <art-cmt :art-id='id'></art-cmt>
  </div>
</template>

<script>
import hljs from 'highlight.js'
import { getArticleDetailAPI, folloWingsAPI, unfollowUserAPI, addLikeAPI, delLikeAPI } from '@/API/articles.js'
import ArtCmt from '@/components/ArtCmt/Art-Cmt.vue'
export default {
  components: {
    ArtCmt
  },
  props: ['id'],
  data  () {
    return {
      article: null
    }
  },
  methods: {
    // 获取文章详情
    async initArticle () {
      const { data: res } = await getArticleDetailAPI(this.id)
      if (res.message === 'OK') {
        this.article = res.data
        console.log(this.article)
      }
    },
    // 关注作者
    async setFollow () {
      await folloWingsAPI(this.article.aut_id.toString())

      this.article.is_followed = true
      this.$toast.success('关注成功')
    },
    // 取关作者
    async unfollowUser () {
      await unfollowUserAPI(this.article.aut_id.toString())

      this.article.is_followed = false
      this.$toast.success('取消关注')
    },
    // 给文章点赞
    async addLike () {
      await addLikeAPI(this.article.art_id.toString())

      this.$toast.success('点赞成功')
      this.article.attitude = 1 // 标签变成已点赞状态
    },
    // 取消点赞
    async delLike () {
      await delLikeAPI(this.article.art_id.toString())

      this.$toast.success('取消点赞')
      this.article.attitude = -1 // 标签变成没点赞状态
    }
  },
  created () {
    this.initArticle()
  },
  updated () { // 在这个钩子函数中不会因为ajax问题出现高亮不好用的原因
    if (this.article) {
    // 3. 对文章的内容进行高亮处理
      hljs.highlightAll()
    }
  }
}
</script>

<style lang="less" scoped>
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
